/*
 * --------------------
 * Company					: LUOYANG GINGKO TECHNOLOGY CO.,LTD.
 * BBS						: http://www.eeschool.org
 * --------------------
 * Project Name			: USART
 * Module Name				: TXD_RXD_Ctrl
 * Description				: The codes of "TXD_RXD_Ctrl"
 * --------------------
 * Tool Versions			: Quartus II 13.1
 * Target Device			: Cyclone IV E  EP4CE10F17C8
 * --------------------
 * Engineer					: xiaorenwu
 * Revision					: V0.0
 * Created Date			: 2016-04-06
 * --------------------
 * Engineer					:
 * Revision					:
 * Modified Date			:
 * --------------------
 * Additional Comments	:
 *
 * --------------------
 */

//--------------------Timescale------------------------------//
`timescale 1 ns / 1 ps
//--------------------Module_TXD_RXD_Ctrl-----------------------//
	module Motion_Ctrl(
						input CLK_25M,
						input WR,
						input RD,
						input CS0,
						input AA,
						input BB,
						input CC,

						input rst_n,
						input BPS_CLK,
						inout [15:0]DB,
						input [24:16]address,
						
						output FPGA_LEDR,FPGA_LEDG,FPGA_LEDB,
						output A1_1,A2_1,B1_1,B2_1,//电机1
						output A1_2,A2_2,B1_2,B2_2,//电机2
						output A1_3,A2_3,B1_3,B2_3,//电机3
						output A1_4,A2_4,B1_4,B2_4,//电机4
						output [1:0]wire_state
						);
wire rd = (CS0 | RD);		 //提取读信号		
wire wr = (CS0 | WR);       //提取写信号

assign DB = !rd ? (address[24:16] == 9'b001110101 ? outRunState : outDataIn) : 16'hzzzz;//16位数据线为高阻状态	

//----------------encode----------------------//
//reg [15:0] encode_count = 15'b0;
//	reg encode_flag = 1'b0;
//
//	always @ (posedge BPS_CLK or negedge rst_n)
//	begin	
//		if(!rst_n)
//			begin
//				encode_flag <= 1'b0;
//			end
//		else
//			begin
//				encode_flag <= AA;
//			end
//	end
//					
//	always @ (posedge BPS_CLK or negedge rst_n)
//	begin	
//		if(!rst_n)
//			begin
//				encode_count <= 15'b0;
//			end
//		else
//			begin
//				if(encode_flag != AA)
//					begin
//						encode_count <= encode_count + 1'b1;
//					end
//			end
//	end
//	assign DB = !rd ? encode_count : DB;	
// always@(posedge clk,posedge rst)
//	 begin
//        if(rst)
//				begin
//                A_Debounce_reg <= 1'b1;
//            end
//        else 
//				begin
//                A_Debounce_reg <= A_Debounce;
//            end
//    end
//        
//assign A_pos = !A_Debounce_reg && A_Debounce;
//assign A_neg = A_Debounce_reg && !A_Debounce;
        
//always@(posedge clk,posedge rst)
//	begin
//       if(rst)
//				begin
//                rotary_right <= 1'b1;
//                rotary_left <= 1'b1;
//            end
//        else 
//				begin
//                if(A_pos && !B_Debounce)//A的上升沿时候如果B为低电平，则旋转编码器是向右转
//						 begin
//                     encode_count <= encode_count + 1'b1;
//                   end
//                 if(A_pos && B_Debounce)//A上升沿时候如果B为高电平，则旋转编码器是向左转
//					    begin
//                      encode_count <= encode_count - 1'b1;
//                   end
//             end
//   end
//
//always@(posedge clk,posedge rst)
//	begin
//		 if(rst)
//			begin
//				rotary_right_reg <= 1'b1;
//				rotary_left_reg <= 1'b1;
//			end
//		 else 
//			begin
//				rotary_right_reg <= rotary_right;
//				rotary_left_reg <= rotary_left;
//			end
//	end
//
//assign rotary_right_pos = !rotary_right_reg && rotary_right;
//assign rotary_left_pos = !rotary_left_reg && rotary_left;
//
//assign rotary_event = rotary_right_pos || rotary_left_pos;
			
//----------------led----------------------//					
reg [2:0] led = 3'b111;
assign {FPGA_LEDR,FPGA_LEDG,FPGA_LEDB}	=led;

//--------------存储数据-----------//
 reg [15:0] dataIn;
 reg [15:0] outDataIn;
	always @ (posedge CLK_25M or negedge rst_n)
	begin	
		if(!rst_n)
			begin
				dataIn <= 16'b0;
			end
		else
			begin
				if(!wr)
					begin
						dataIn <= DB;
					end
			end
	end
	
	
	always @ (posedge CLK_25M or negedge rst_n)
	begin	
		if(!rst_n)
			begin
				outDataIn <= 16'b0;
			end
		else
			begin
				outRunState <= runState;
				outDataIn <= dataIn;
			end
	end
//--------------------------数据解析---------------------------//
	localparam 	led_red = 3'b011, led_green = 3'b101, led_blue = 3'b110, led_white = 3'b000, led_other = 3'b001,
	
					red = 4'b0001,green = 4'b0010,blue = 4'b0011,
					
					taida_go = 4'b0100,//台达电机伸出
					taida_back = 4'b0101,//台达电机缩回
					stop = 4'b0110,
					
					//三种速度
					taida_slow = 4'b0111,
					taida_mid = 4'b1000,
					taida_fast = 4'b1001,
					
					taida_touch =  4'b1010,//点动
					taida_linkmove = 4'b1011,//联动
					
					plusCountBin = 4'b1100,//脉冲计数信息
					freqCountBin = 4'b1101,//频率计数信息
					MOVE =    4'b1110,//电机参数发送结束，可以开始运动

					//4个电机
					moto1_flag = 2'b00,
					moto2_flag = 2'b01,
					moto3_flag = 2'b10,
					moto4_flag = 2'b11,
					
					direction_flag = 3'b000,//方向
					frag1_flag = 	  3'b001,//电机脉冲的四个片段
					frag2_flag =     3'b010,
					frag3_flag =     3'b011,
					frag4_flag =     3'b100,
					frag_over =      3'b101,//发完了
					
					norunning = 16'b1111101010101010,
					running = 16'b0;//正在走步

	reg[1:0] state = 2'b0;//速度状态
	assign wire_state[1:0] = state;
	
	reg[3:0] frag1 = 4'b0;//4个4位
	reg[3:0] frag2 = 4'b0;
	reg[3:0] frag3 = 4'b0;
	reg[3:0] frag4 = 4'b0;
	
	reg[15:0] plusCount1 = 16'b0;//1号电机要运动的脉冲数
	reg[15:0] plusCount2 = 16'b0;
	reg[15:0] plusCount3 = 16'b0;
	reg[15:0] plusCount4 = 16'b0;
	
	reg[15:0] count1 = 16'b0;//电机的脉冲计数
	reg[15:0] count2 = 16'b0;
	reg[15:0] count3 = 16'b0;
	reg[15:0] count4 = 16'b0;
	
	reg[15:0] freqCount1 = 16'b0;//1号电机的频率计数目标值
	reg[15:0] freqCount2 = 16'b0;
	reg[15:0] freqCount3 = 16'b0;
	reg[15:0] freqCount4 = 16'b0;
	
	reg[15:0] fCount1 = 16'b0;//1号电机的频率计数
	reg[15:0] fCount2 = 16'b0;
	reg[15:0] fCount3 = 16'b0;
	reg[15:0] fCount4 = 16'b0;
	
	reg kk = 1'b0;
	
	reg[15:0] runState = norunning;
	
	reg[15:0] outRunState = norunning;
		
	always @ (posedge BPS_CLK or negedge rst_n)
		begin
			if(!rst_n)
				begin
					led <= 3'b000;
				end
			else 
				begin
					if(address[24:16] == 9'b001011000)
						begin
							case(dataIn[15:12])
								taida_touch:
									begin
										move_pattern <= 1'b0;
									end
								taida_linkmove:
									begin
										move_pattern <= 1'b1;
									end
								taida_slow:
									begin
										state <= 2'b00;
									end
								taida_mid:
									begin
										state <= 2'b01;
									end
								taida_fast:
									begin
										state <= 2'b10;
									end
								red:
									begin
										led <= led_red;
									 end
								green:
									begin
										led <= led_green;
									end
								blue:
									begin
										led <= led_blue;
									end
							endcase
							
							if(move_pattern)//联动模式
								begin	
									case(dataIn[15:12])
										plusCountBin:
											begin
												case(dataIn[11:10])
													moto1_flag:
														begin
															case(dataIn[9:7])
																direction_flag:
																	begin
																		taida_direction1 <= dataIn[6];
																	end
																frag1_flag:
																	begin
																		frag1 <= dataIn[3:0];
																		led <= led_red;
																	end
																frag2_flag:
																	begin
																		frag2 <= dataIn[3:0];
																		led <= led_green;
																	end
																frag3_flag:
																	begin
																		frag3 <= dataIn[3:0];
																		led <= led_blue;
																	end
																frag4_flag:
																	begin
																		frag4 <= dataIn[3:0];
																		led <= led_white;
																	end
																frag_over:
																	begin
																		plusCount1 <= {frag1, frag2, frag3, frag4};
																		count1 <= 16'b0;
																		led <= led_other;
																	end
															endcase
														end
													moto2_flag:
														begin
															case(dataIn[9:7])
																direction_flag:
																	begin
																		taida_direction2 <= dataIn[6];
																	end
																frag1_flag:
																	begin
																		frag1 <= dataIn[3:0];
																		led <= led_red;
																	end
																frag2_flag:
																	begin
																		frag2 <= dataIn[3:0];
																		led <= led_green;
																	end
																frag3_flag:
																	begin
																		frag3 <= dataIn[3:0];
																		led <= led_blue;
																	end
																frag4_flag:
																	begin
																		frag4 <= dataIn[3:0];
																		led <= led_white;
																	end
																frag_over:
																	begin
																		plusCount2 <= {frag1, frag2, frag3, frag4};
																		count2 <= 16'b0;
																		led <= led_other;
																	end
															endcase
														end
													moto3_flag:
														begin
															case(dataIn[9:7])
																direction_flag:
																	begin
																		taida_direction3 <= dataIn[6];
																	end
																frag1_flag:
																	begin
																		frag1 <= dataIn[3:0];
																		led <= led_red;
																	end
																frag2_flag:
																	begin
																		frag2 <= dataIn[3:0];
																		led <= led_green;
																	end
																frag3_flag:
																	begin
																		frag3 <= dataIn[3:0];
																		led <= led_blue;
																	end
																frag4_flag:
																	begin
																		frag4 <= dataIn[3:0];
																		led <= led_white;
																	end
																frag_over:
																	begin
																		plusCount3 <= {frag1, frag2, frag3, frag4};
																		count3 <= 16'b0;
																		led <= led_other;
																	end
															endcase
														end
													moto4_flag:
														begin
															case(dataIn[9:7])
																direction_flag:
																	begin
																		taida_direction4 <= dataIn[6];
																	end
																frag1_flag:
																	begin
																		frag1 <= dataIn[3:0];
																		led <= led_red;
																	end
																frag2_flag:
																	begin
																		frag2 <= dataIn[3:0];
																		led <= led_green;
																	end
																frag3_flag:
																	begin
																		frag3 <= dataIn[3:0];
																		led <= led_blue;
																	end
																frag4_flag:
																	begin
																		frag4 <= dataIn[3:0];
																		led <= led_white;
																	end
																frag_over:
																	begin
																		plusCount4 <= {frag1, frag2, frag3, frag4};
																		count4 <= 16'b0;
																		led <= led_other;
																	end
															endcase
														end
													endcase
											end
										freqCountBin:
											begin
												case(dataIn[11:10])
													moto1_flag:
														begin
															case(dataIn[9:7])
																frag1_flag:
																	begin
																		frag1 <= dataIn[3:0];
																		led <= led_red;
																	end
																frag2_flag:
																	begin
																		frag2 <= dataIn[3:0];
																		led <= led_green;
																	end
																frag3_flag:
																	begin
																		frag3 <= dataIn[3:0];
																		led <= led_blue;
																	end
																frag4_flag:
																	begin
																		frag4 <= dataIn[3:0];
																		led <= led_white;
																	end
																frag_over:
																	begin
																		freqCount1 <= {frag1, frag2, frag3, frag4};
																		fCount1 <= 16'b0;
																		led <= led_other;
																	end
															endcase
														end
													moto2_flag:
														begin
															case(dataIn[9:7])
																frag1_flag:
																	begin
																		frag1 <= dataIn[3:0];
																		led <= led_red;
																	end
																frag2_flag:
																	begin
																		frag2 <= dataIn[3:0];
																		led <= led_green;
																	end
																frag3_flag:
																	begin
																		frag3 <= dataIn[3:0];
																		led <= led_blue;
																	end
																frag4_flag:
																	begin
																		frag4 <= dataIn[3:0];
																		led <= led_white;
																	end
																frag_over:
																	begin
																		freqCount2 <= {frag1, frag2, frag3, frag4};
																		fCount2 <= 16'b0;
																		led <= led_other;
																	end
															endcase
														end
													moto3_flag:
														begin
															case(dataIn[9:7])
																frag1_flag:
																	begin
																		frag1 <= dataIn[3:0];
																		led <= led_red;
																	end
																frag2_flag:
																	begin
																		frag2 <= dataIn[3:0];
																		led <= led_green;
																	end
																frag3_flag:
																	begin
																		frag3 <= dataIn[3:0];
																		led <= led_blue;
																	end
																frag4_flag:
																	begin
																		frag4 <= dataIn[3:0];
																		led <= led_white;
																	end
																frag_over:
																	begin
																		freqCount3 <= {frag1, frag2, frag3, frag4};
																		fCount3 <= 16'b0;
																		led <= led_other;
																	end
															endcase
														end
													moto4_flag:
														begin
															case(dataIn[9:7])
																frag1_flag:
																	begin
																		frag1 <= dataIn[3:0];
																		led <= led_red;
																	end
																frag2_flag:
																	begin
																		frag2 <= dataIn[3:0];
																		led <= led_green;
																	end
																frag3_flag:
																	begin
																		frag3 <= dataIn[3:0];
																		led <= led_blue;
																	end
																frag4_flag:
																	begin
																		frag4 <= dataIn[3:0];
																		led <= led_white;
																	end
																frag_over:
																	begin
																		freqCount4 <= {frag1, frag2, frag3, frag4};
																		fCount4 <= 16'b0;
																		led <= led_other;
																		kk <= 1'b1;
																	end
															endcase
														end
													endcase
											end
										MOVE:
											begin
												if(kk)
													begin
														GO <= 1'b1;
														GO1 <= 1'b0;
														GO2 <= 1'b0;
														GO3 <= 1'b0;
														GO4 <= 1'b0;
														kk <= 1'b0;
														led <= led_white;
														freqCount1 <= freqCount1 - 1'b1;
														freqCount2 <= freqCount2 - 1'b1;
														freqCount3 <= freqCount3 - 1'b1;
														freqCount4 <= freqCount4 - 1'b1;
														count1 <= 16'b0;
														count2 <= 16'b0;
														count3 <= 16'b0;
														count4 <= 16'b0;
														fCount1 <= 16'b0;
														fCount2 <= 16'b0;
														fCount3 <= 16'b0;
														fCount4 <= 16'b0;
													end
											end
									endcase									
								end
							else//点控
								begin
									case(dataIn[15:12])
										stop:
											begin
												case(dataIn[1:0])
													moto1_flag:
														begin
															GO1 <= 1'b0;
														end
													moto2_flag:
														begin
															GO2 <= 1'b0;
														end
													moto3_flag:
														begin
															GO3 <= 1'b0;
														end
													moto4_flag:
														begin
															GO4 <= 1'b0;
														end
												endcase
											end
										taida_go:
											begin
												case(dataIn[1:0])
													moto1_flag:
														begin
															taida_direction1 <= 1'b1;
															GO1 <= 1'b1;
														end
													moto2_flag:
														begin
															taida_direction2 <= 1'b1;
															GO2 <= 1'b1;
														end
													moto3_flag:
														begin
															taida_direction3 <= 1'b1;
															GO3 <= 1'b1;
														end
													moto4_flag:
														begin
															taida_direction4 <= 1'b1;
															GO4 <= 1'b1;
														end
												endcase
											end
										taida_back:
											begin
												case(dataIn[1:0])
													moto1_flag:
														begin
															taida_direction1 <= 1'b0;
															GO1 <= 1'b1;
														end
													moto2_flag:
														begin
															taida_direction2 <= 1'b0;
															GO2 <= 1'b1;
														end
													moto3_flag:
														begin
															taida_direction3 <= 1'b0;
															GO3 <= 1'b1;
														end
													moto4_flag:
														begin
															taida_direction4 <= 1'b0;
															GO4 <= 1'b1;
														end
												endcase
											end
										default:;
									endcase
								end							
						end
					if(GO)
						begin
							runState <= running;
							led <= led_red;
							if(count1 < plusCount1)//脉冲计数
								begin
									if(fCount1 < freqCount1)//频率计数
										begin
											fCount1 <= fCount1 + 1'b1;
											GO1 <= 1'b0;
										end
									else
										begin
											count1 <= count1 + 1'b1;
											GO1 <= 1'b1;
											fCount1 <= 16'b0;
										end
								end
							else
								begin
									GO1 <= 1'b0;
								end
								
							if(count2 < plusCount2)
								begin
									if(fCount2 < freqCount2)
										begin
											fCount2 <= fCount2 + 1'b1;
											GO2 <= 1'b0;
										end
									else
										begin
											count2 <= count2 + 1'b1;
											GO2 <= 1'b1;
											fCount2 <= 16'b0;
										end
								end
							else
								begin
									GO2 <= 1'b0;
								end
								
							if(count3 < plusCount3)
								begin
									if(fCount3 < freqCount3)
										begin
											fCount3 <= fCount3 + 1'b1;
											GO3 <= 1'b0;
										end
									else
										begin
											count3 <= count3 + 1'b1;
											GO3 <= 1'b1;
											fCount3 <= 16'b0;
										end
								end
							else
								begin
									GO3 <= 1'b0;
								end
								
							if(count4 < plusCount4)
								begin
									if(fCount4 < freqCount4)
										begin
											fCount4 <= fCount4 + 1'b1;
											GO4 <= 1'b0;
										end
									else
										begin
											count4 <= count4 + 1'b1;
											GO4 <= 1'b1;
											fCount4 <= 16'b0;
										end
								end
							else
								begin
									GO4 <= 1'b0;
								end

							
							if(count1 == plusCount1 && count2 == plusCount2 && count3 == plusCount3 && count4 == plusCount4)
								begin
									GO <= 1'b0;
									count1 <= 16'b0;
									count2 <= 16'b0;
									count3 <= 16'b0;
									count4 <= 16'b0;
									GO1 <= 1'b0;
									GO2 <= 1'b0;
									GO3 <= 1'b0;
									GO4 <= 1'b0;
									fCount1 <= 16'b0;
									fCount2 <= 16'b0;
									fCount3 <= 16'b0;
									fCount4 <= 16'b0;
								end
						end
					else
						begin
							runState <= norunning;
							led <= led_green;
						end
				end
		end
		
//--------------------------电机输出参数
//---------------------------台达-------------------------------//
	//localparam flag = 15'b100111000100000;//20000脉冲5毫米
	reg move_pattern = 1'b0;//0表示点控，1表示指定长度
	
	assign {A1_1,A2_1,B1_1,B2_1} = moto1;//D12,C14,G15,F16
	assign {A1_2,A2_2,B1_2,B2_2} = moto2;//G2,D1,B1,F2,
	assign {A1_3,A2_3,B1_3,B2_3} = moto3;//E5,F3,E6,D3
	assign {A1_4,A2_4,B1_4,B2_4} = moto4;//C16,D15,A15,B16
	reg [3:0] moto1;
	reg [3:0] moto2;
	reg [3:0] moto3;
	reg [3:0] moto4;
	reg GO = 1'b0, GO1 = 1'b0, GO2 = 1'b0, GO3 = 1'b0, GO4 = 1'b0;//四个电机的使能
	reg pluse1 = 1'b1, pluse2 = 1'b1, pluse3 = 1'b1, pluse4 = 1'b1;
	reg taida_direction1 = 1'b0, taida_direction2 = 1'b0, taida_direction3 = 1'b0, taida_direction4 = 1'b0;
	
	//------------1号电机
	always@(posedge GO1 or negedge rst_n)
		begin
			if(!rst_n)
				begin
					moto1 <= 4'b1001;
				end
			else
				begin
//					if(GO1)
//						begin
							if(taida_direction1)
								begin
									if(pluse1)
										begin
											moto1 <= 4'b1010;
											pluse1 <= ~pluse1;
										end
									else
										begin
											moto1 <= 4'b0110;
											pluse1 <= ~pluse1;
										end
								end
							else
								begin
									if(pluse1)
										begin
											moto1 <= 4'b1001;
											pluse1 <= ~pluse1;
										end
									else
										begin
											moto1 <= 4'b0101;
											pluse1 <= ~pluse1;
										end
								end
//						end
				end
		end
		
	//------------2号电机
	always@(posedge GO2 or negedge rst_n)
		begin
			if(!rst_n)
				begin
					moto2 <= 4'b1001;
				end
			else
				begin
//					if(GO2)
//						begin
							if(taida_direction2)
								begin
									if(pluse2)
										begin
											moto2 <= 4'b1010;
											pluse2 <= ~pluse2;
										end
									else
										begin
											moto2 <= 4'b0110;
											pluse2 <= ~pluse2;
										end
								end
							else
								begin
									if(pluse2)
										begin
											moto2 <= 4'b1001;
											pluse2 <= ~pluse2;
										end
									else
										begin
											moto2 <= 4'b0101;
											pluse2 <= ~pluse2;
										end
								end
//						end
				end
		end
		
			//------------3号电机
	always@(posedge GO3 or negedge rst_n)
		begin
			if(!rst_n)
				begin
					moto3 <= 4'b1001;
				end
			else
				begin
//					if(GO3)
//						begin
							if(taida_direction3)
								begin
									if(pluse3)
										begin
											moto3 <= 4'b1010;
											pluse3 <= ~pluse3;
										end
									else
										begin
											moto3 <= 4'b0110;
											pluse3 <= ~pluse3;
										end
								end
							else
								begin
									if(pluse3)
										begin
											moto3 <= 4'b1001;
											pluse3 <= ~pluse3;
										end
									else
										begin
											moto3 <= 4'b0101;
											pluse3 <= ~pluse3;
										end
								end
//						end
				end
		end
		
					//------------4号电机
	always@(posedge GO4 or negedge rst_n)
		begin
			if(!rst_n)
				begin
					moto4 <= 4'b1001;
				end
			else
				begin
//					if(GO4)
//						begin
							if(taida_direction4)
								begin
									if(pluse4)
										begin
											moto4 <= 4'b1010;
											pluse4 <= ~pluse4;
										end
									else
										begin
											moto4 <= 4'b0110;
											pluse4 <= ~pluse4;
										end
								end
							else
								begin
									if(pluse4)
										begin
											moto4 <= 4'b1001;
											pluse4 <= ~pluse4;
										end
									else
										begin
											moto4 <= 4'b0101;
											pluse4 <= ~pluse4;
										end
								end
//						end
				end
		end


//---------------------------步进电机---------------------------//
	/*
	reg [2:0] step;
	localparam step1=3'b000,step2=3'b001,step3=3'b010,step4=3'b011,step5=3'b100,step6=3'b101,step7=3'b110,step8=3'b111;
	
	reg [7:0] delay = 8'b0;
	//reg [7:0] delayTime = 8'b00000100;
	reg [7:0] delayTime = 8'b00110000;//震荡频率除以50就是电机转频，现在一秒震荡200次
	reg [7:0] countSUM = 8'b11111111;
	reg [7:0] count = 8'b0;
	
	reg true = 1'b1;
	reg _GO = 1'b1;
		
	reg MotionModule = 1'b1;
	
	always@(posedge BPS_CLK or negedge rst_n)
		begin
			if(!rst_n)
				begin
					moto <= 4'b1111;
					step <= step1;
					delay <= 8'b0;
				end
			else
				begin
					if(MotionModule)
						begin
							if(GO)
								begin							
									if(delay < delayTime)
										begin
											delay <= delay + 1'b1;
										end
									else
										begin
											_GO <= 1'b1;
											delay <= 8'b0;
											case (step)
												step1:begin
															moto <= 4'b1000;
															if(true) step <= step2;
															else step <= step8;
														end
												step2:begin
															moto <= 4'b1010;
															if(true) step <= step3;
															else step <= step1;
														end
												step3:begin
															moto <= 4'b0010;
															if(true) step <= step4;
															else step <= step2;
														end
												step4:begin
															moto <= 4'b0110;
															if(true) step <= step5;
															else step <= step3;
														end
												step5:begin
															moto <= 4'b0100;
															if(true) step <= step6;
															else step <= step4;
														end
												step6:begin
															moto <= 4'b0101;
															if(true) step <= step7;
															else step <= step5;
														end
												step7:begin
															moto <= 4'b0001;
															if(true) step <= step8;
															else step <= step6;
														end
												step8:begin
															moto <= 4'b1001;
															if(true) step <= step1;
															else step <= step7;
														end
												default:step <= step1;
											endcase
										end
								end
						end
					else
						begin
							if(_GO)
								begin												
									if(delay < delayTime)
										begin
											delay <= delay + 1'b1;
										end
									else
										begin
											if(count < countSUM)
												begin
													count <= count + 1'b1;
												end
											else if(count >= countSUM)
												begin
													count <= 1'b0;
													_GO <= 1'b0;
												end	
										
											delay <= 8'b0;
											case (step)
												step1:begin
															moto <= 4'b1000;
															if(true) step <= step2;
															else step <= step8;
														end
												step2:begin
															moto <= 4'b1010;
															if(true) step <= step3;
															else step <= step1;
														end
												step3:begin
															moto <= 4'b0010;
															if(true) step <= step4;
															else step <= step2;
														end
												step4:begin
															moto <= 4'b0110;
															if(true) step <= step5;
															else step <= step3;
														end
												step5:begin
															moto <= 4'b0100;
															if(true) step <= step6;
															else step <= step4;
														end
												step6:begin
															moto <= 4'b0101;
															if(true) step <= step7;
															else step <= step5;
														end
												step7:begin
															moto <= 4'b0001;
															if(true) step <= step8;
															else step <= step6;
														end
												step8:begin
															moto <= 4'b1001;
															if(true) step <= step1;
															else step <= step7;
														end
												default:step <= step1;
											endcase
										end
								end

						end
				end	
		end
		*/
//--------------------Endmodule---------------------------------//

	endmodule
